{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d90d972e",
   "metadata": {},
   "source": [
    "# 1.Python程序编写方式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bdfa985",
   "metadata": {},
   "source": [
    "#### Python是一种解释型的脚本编程语言\n",
    "支持两种代码编写方式：\n",
    "\n",
    "1.交互命令式方式，也称交互模式，相当于启动Python**解释器**，每输入一行就执行一行。\n",
    "\n",
    "2..py代码文件方式，运行.py文件就相当于启动了Python解释器，然后**一次性把.py文件的源代码给执行了**。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2617b280",
   "metadata": {},
   "source": [
    "### 1.1 交互命令行方式编程\n",
    "> 在命令行窗口直接输入代码，按下回车键就可以运行代码，并立即看到输出结果。\n",
    "> 首先要进入python交互式编程环境（方式方法多样）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "792a6aa0",
   "metadata": {},
   "source": [
    "### 1.2 编写python源文件（.py源文件）\n",
    "> 1.创建一个源文件，将所有代码放在源文件中，\n",
    "让解释器***逐行***读取并执行源文件中的代码，直到文件末尾，也就是**批量执行代码**。这是最常见的编程方式。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b8008fe",
   "metadata": {},
   "source": [
    "* python文件在集成环境中执行如双击运行闪退？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "06b92845",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！\n",
      "这是我的测试\n",
      "按任意按键退出8\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'8'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"你好！\")\n",
    "print(\"这是我的测试\")\n",
    "input(\"按任意按键退出\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9a79ed1",
   "metadata": {},
   "outputs": [],
   "source": [
    " # 其实程序是运行了，运行完之后就把显示的窗口关掉了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e2c0328",
   "metadata": {},
   "source": [
    " * 在程序末尾加input()函数（等待输入的函数），如：input(\"按任意按键退出\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a289cafd",
   "metadata": {},
   "source": [
    "# 2.Python编码规范"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17fa9820",
   "metadata": {},
   "source": [
    "## 2.1代码编码格式\n",
    "* 一般来说，声明编码格式在脚本中是必须的\n",
    "* 国际惯例，文件编码和python编码格式全部为utf-8。例如：在python代码的开头，要统一加上如下代码："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0ad140f3",
   "metadata": {},
   "outputs": [],
   "source": [
    " # -- coding: utf-8 --"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb42c220",
   "metadata": {},
   "source": [
    "* 如果python源码文件没有声明编码格式，python解释器会默认使用ASCII编码的字符，python解释器就会报错，因此非ASCII字符的字符串，请需添加**u前缀**"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6616af0c",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "9601ce8f",
   "metadata": {},
   "source": [
    "* 若出现python编码问题，可按照以下操作尝试解决："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5a0f8272",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'reload' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[20], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msys\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m reload(sys)\n\u001b[0;32m      3\u001b[0m sys\u001b[38;5;241m.\u001b[39msetdefaultencoding(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mutf-8\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'reload' is not defined"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "reload(sys)\n",
    "sys.setdefaultencoding('utf-8')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdcdd89b",
   "metadata": {},
   "source": [
    "## 2.2 分号（不要在行尾加分号，也不要用分号将两条命令放在同一行）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28179f22",
   "metadata": {},
   "source": [
    "## 2.3 行的最大长度\n",
    "* 每行不超过80个字符，以下情况除外：\n",
    "> 1.长的导入模块语句\n",
    "> 2.注释里的URL"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "894beb86",
   "metadata": {},
   "source": [
    "* 不要使用反斜杠连接行。\n",
    "Python会将**圆括号、中括号和花括号中的行隐式**连接起来\n",
    "圆括号、方括号或花括号以内的表达式允许分成多个物理行，无需使用反斜杠。例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "9d6107ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "month_names = ['januari','februari',        # There are the\n",
    "               'maart','mei',              # dutch names\n",
    "              'december']                  # for the months"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "389bfc6a",
   "metadata": {},
   "source": [
    "* 隐式的行接式拼接可以带有注释。后续行的缩进不影响程序结构。后续行也允许为空白行。\n",
    "* 如果需要，可以在表达式外围增加一堆额外的圆括号。\n",
    "* 如果一个文本字符串在一行放不下，可以使用圆括号来实现**隐式连接**。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "70c5342a",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = ('这是一个非常长非常长非常长非常长非常长非常长非常长非常长'\n",
    "     '非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长非常长的字符串')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a23315ad",
   "metadata": {},
   "source": [
    "## 2.4 缩进规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f301e153",
   "metadata": {},
   "source": [
    "* PYthon采用代码缩进和冒号(:)来区分代码块之间的层次\n",
    "* 在python中，对于类定义、函数定义、流程控制语句、异常处理语句等，行尾的冒号和下一行的缩进，表示下一个代码块的开始，而缩进结束则表示此代码块的结束。\n",
    "* python中实现对代码的缩进，可以使用*空格*或者*Tab键*，通常情况都是采用4个空格长度作为一个缩进量（默认情况下，一个Tab键就表示4个空格）。\n",
    "* 对于python缩进规则，初学者可以这样理解，python要求属于**同一作用域**中的各行代码，它们的缩进量必须一致，但具体缩进量为多少，并不做硬性规定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0fce6851",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'a' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[35], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m a\u001b[38;5;241m==\u001b[39m\u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m      2\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m正确\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      3\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
      "\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
     ]
    }
   ],
   "source": [
    "if a==0:\n",
    "    print(\"正确\")\n",
    "else:\n",
    "    print(\"错误\")\n",
    " \n",
    "\n",
    "foo = long_function_name(\n",
    "     var_one,var_two)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1560269",
   "metadata": {},
   "source": [
    "## 2.5 注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a37d7dc",
   "metadata": {},
   "source": [
    "* python中使用# 进行注释，# 号后面要空一格。\n",
    "* 为提高可读性，注释和代码隔开一定的距离，注释至少离开代码2个空格，块注释后面最好多留几行空白再写代码。\n",
    "* 在句尾结束的时候应该使用两个空格  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62755bc9",
   "metadata": {},
   "source": [
    "* python中有三种形式的注释：行注释、块注释、文档注释\n",
    "> 1.行注释：解释自己做了什么，而不是对代码本身的解释\n",
    "行内注释是与代码语句同行的注释\n",
    "行内注释和代码至少要有两个空格分隔\n",
    "注释由#和一个空格开始 例如： # 这就是注释\n",
    "\n",
    "> 2.块注释：通常跟随他们的某些（全部）代码，并缩进到与代码相同的级别\n",
    "块注释内部的段落通常只有一个#的空行分隔\n",
    "\n",
    "> 3.文档注释：要为所有的公共模块、函数、类和方法编写文档说明\n",
    "非公共的方法没有必要，但是应该有一个描述方法具体作用的注释。\n",
    "多行文档注释使用的结尾三引号应该是自成一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "95f4b9ac",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Sampleclass(object):\n",
    "    \"\"\"summary of class here.\n",
    "    eggs: an egg\n",
    "    \"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d65f2b6",
   "metadata": {},
   "source": [
    "## 2.6 空行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6da82df2",
   "metadata": {},
   "source": [
    "* 顶层函数和类定义，前后用两个空行隔开\n",
    "* 编码格式声明、模块导入、常量和全局变量声明、顶级定义和执行代码之间空两行\n",
    "* 类里面方法定义用一个空行隔开\n",
    "* 在函数或方法内部，可以在必要的地方空一行以增强节奏感，但应避免连续空行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8b4e4960",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Clss01:\n",
    "    pass\n",
    "\n",
    "\n",
    "class CLass02:\n",
    "    def function_01(self):\n",
    "        pass\n",
    "    \n",
    "    def function_02(self):\n",
    "        pass\n",
    "# 使用必要的空行可以增加代码的可读性，通常在顶级定义（如函数或类的定义）之间空两行，而方法之间空一行，另外在用于分割某些功能的位置也可以空一行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6a27ac6",
   "metadata": {},
   "source": [
    "## 2.7 制表符还是空格"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a32ee5",
   "metadata": {},
   "source": [
    "* 不要将两者混用，虽然在编辑环境中显示两条语句为同一缩进层次，但因为制表符和空格的不同会导致python解释为两个不同的层次。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee1ac35a",
   "metadata": {},
   "source": [
    "* 强烈推荐仅使用空格而不是制表符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b548a203",
   "metadata": {},
   "source": [
    "### 空格使用规则："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3f542cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
